<?php
/*---------------------------------------------------------------------------
  小微OA系统 - 让工作更轻松快乐 

  Copyright (c) 2013 http://www.smeoa.com All rights reserved.                                             


  Author:  jinzhu.yin<smeoa@qq.com>                         

  Support: https://git.oschina.net/smeoa/xiaowei               
 -------------------------------------------------------------------------*/


namespace Home\Model;
use Think\Model;

class  CrmContactModel extends CommonModel {
	
	function set_share($contact_list,$user_list){
		if (empty($user_list)) {
			return false;
		}
		if (empty($contact_list)) {
			return false;
		}
		if (is_array($user_list)) {
			$user_list = array_filter($user_list);
		} else {
			$user_list = array_filter(explode(",", $user_list));			
		}
		$user_list = implode(",", $user_list);

		if (is_array($contact_list)) {
			$contact_list = array_filter($contact_list);
		} else {
			$contact_list = array_filter(explode(",", $contact_list));			
		}
		$contact_list = implode(",", $contact_list);
		
		$sql='delete from ' .$this -> tablePrefix . 'crm_share where contact_id in(' . $contact_list . ')';
		$this -> execute($sql);
 		
		$where = 'a.id in (' . $user_list . ') AND b.id in(' . $contact_list . ')';
		$sql = 'INSERT INTO ' . $this -> tablePrefix . 'crm_share (user_id,contact_id) ';
		$sql .= ' SELECT a.id, b.id FROM ' . $this -> tablePrefix . 'user a, ' . $this -> tablePrefix . 'crm_contact b WHERE ' . $where;
				
		$result = $this -> execute($sql);
		if ($result == false){
			return false;
		} else {
			return true;
		}
	}
}
?>